package cn.wj.school.cloud.controller;

import cn.wj.school.cloud.dto.DWZResult;
import cn.wj.school.cloud.dto.PageResponse;
import cn.wj.school.cloud.dto.SyUserDTO;
import cn.wj.school.cloud.dto.condition.UserCondition;
import cn.wj.school.cloud.enums.EcodeType;
import cn.wj.school.cloud.enums.UserType;
import cn.wj.school.cloud.exception.ServiceInvokeException;
import cn.wj.school.cloud.service.EcodeService;
import cn.wj.school.cloud.service.SchoolService;
import cn.wj.school.cloud.service.UserService;
import cn.wj.school.cloud.utils.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: yueding
 * @Description: 用户控制器
 * @Date: Created in 15:09 2019/1/10
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
@Controller
@RequestMapping("/user/*")
public class UserController extends BaseController {

    @Resource
    UserService userService;

    @Resource
    EcodeService ecodeService;

    @Resource
    SchoolService schoolService;

    @RequestMapping("list")
    public String userList(Model model, @ModelAttribute("condition") UserCondition user, String navTabId) {
        /**
         * 填充当前用户的角色
         */
        SyUserDTO loginUser = SecurityUtils.getCurrentUser();
        /**
         * 学校管理员可以看到学校的用户
         */
        Map<String, String> type = UserType.map;
        if (UserType.STUDENT.name().equals(loginUser.getType())) {
            throw ServiceInvokeException.newException("非法的访问");
        }
        if (UserType.ADMIN.name().equals(loginUser.getType())) {
            user.setSchool(SecurityUtils.getCurrentUser().getSchool());
            type.remove(UserType.ADMINISTRAOR);
        }else if (UserType.TEACHER.name().equals(loginUser.getType())) {
            user.setSchool(SecurityUtils.getCurrentUser().getSchool());
            type.remove(UserType.ADMINISTRAOR);
            type.remove(UserType.TEACHER);
            user.setType(UserType.STUDENT.name());
        }
        PageResponse<SyUserDTO> users = userService.getUsersByCondition(user);
        model.addAttribute("userType", type);
        model.addAttribute("page", users);
        model.addAttribute(NAVTABID, navTabId);
        return "user/user_list";
    }

    /**
     * 校验用户card是否存在,存在返回false,不存在返回true
     * @param card
     * @param id
     * @return
     */
    @RequestMapping("checkedUserCardExits")
    @ResponseBody
    public Boolean checkedUserCardExits(String card, Long id) {
        if (StringUtils.isNotEmpty(card)) {
            SyUserDTO userDTO = userService.getUserByUserAccount(card);
            return userDTO == null ? Boolean.TRUE : userDTO.getId().equals(id);
        }
        return true;
    }

    @RequestMapping("view")
    public String userDetail(Model model, Long id, String navTabId) {
        model.addAttribute(NAVTABID, navTabId);
        if (id != null) {
            model.addAttribute("user", userService.getUserByUserId(id));
        }
        return "user/user_view";
    }

    @RequestMapping("add")
    public String addUser(Model model, String navTabId) {
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("grades", ecodeService.getEcodesByType(EcodeType.GRADE.name()));
        model.addAttribute("userType", UserType.map);
        return "user/user_add";
    }

    @RequestMapping("update")
    public String updateUser(Model model, Long id, String navTabId) {

        model.addAttribute(NAVTABID, navTabId);
        if (id != null) {
            model.addAttribute("user", userService.getUserByUserId(id));
        }
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("grades", ecodeService.getEcodesByType(EcodeType.GRADE.name()));
        model.addAttribute("userType", UserType.map);
        return "user/user_add";
    }

    @RequestMapping("save")
    @ResponseBody
    public DWZResult saveOrUpdateUser(SyUserDTO userDTO,
                                      @RequestParam(value="navTabId")String navTabId,
                                      HttpServletRequest request) {
        if (userDTO.getId() != null) {
            userService.modifyUser(userDTO);
        } else {
            userService.saveUser(userDTO);
        }
        String forwardUrl = getForwardUrl(request) + "/list?navTabId=" + navTabId;
        return dialogAjaxDone("操作成功!", navTabId, null, forwardUrl, null);
    }

    @RequestMapping("delete")
    @ResponseBody
    public DWZResult deleteUser(Model model, String ids, String navTabId) {

        model.addAttribute(NAVTABID, navTabId);
        if (StringUtils.isBlank(ids)) {
            return ajaxError("参数异常");
        }
        String[] ress = ids.split(",");
        for (String id : ids.split(",")) {
            if (StringUtils.isNotBlank(id)) {
                userService.removeUser(Long.valueOf(id));
            }
        }
        return navTabAjaxDone(navTabId, null, null, null, "操作成功!", null);
    }
}
